package com.nuanshui.heatedloan.controller.base;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import com.nuanshui.framework.utils.LogUtil;
import com.nuanshui.heatedloan.baseservice.form.StatisticsForm;
import com.nuanshui.heatedloan.constant.RabbitMqQueue;
import com.nuanshui.heatedloan.controller.BaseController;
import com.nuanshui.heatedloan.middleware.rabbitmq.RabbitmqMessageSender;
import com.nuanshui.heatedloan.middleware.rabbitmq.message.RabbitmqMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/statistics")
public class StatisticsController extends BaseController {

	@Autowired
	private RabbitmqMessageSender rabbitmqMessageSender;
	
	private Logger logger = LoggerFactory.getLogger(getClass());
	
	/**
	 * 统计数据保存
	 * @return
	 */
	@RequestMapping("/save")
	public String save(StatisticsForm form) {
		LogUtil.info(logger, "保存统计数据,要操作的表:" + form.getEventId());
		if(!isFromWechat() && form.getType()==StatisticsForm.TYPE_SINGLE) {
			JSONObject jsonObject = JSON.parseObject(form.getParams());
			jsonObject.put("uid", getSafeToken());
			   form.setParams(JSON.toJSONString(jsonObject));
		}

		form.setUserAgent(getUserAgent());
		rabbitmqMessageSender.send(RabbitMqQueue.RABBIT_MQ_STATISTICS,form);
		return success();
	}
}
